#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int N, K;
    cin >> N >> K;

    vector<int> v(N);
    for (int i = 0; i < N; ++i) {
        cin >> v[i];
    }

    sort(v.begin(), v.end());
    int size = K;
    if (K > N / 2)
        size = N - K;

    vector<int> flag(N);
    int left = N / 2;
    int right = left + 1;
    while (size) {
        if (left >= 0) {
            flag[left--] = 1;
            size--;
        }
        if (size && right < N) {
            flag[right++] = 1;
            size--;
        }
    }

    vector<int> xiaoAI, xiaoXI;
    xiaoAI.reserve(N);
    xiaoXI.reserve(N);

    for (int i = 0; i < N; ++i) {
        if (flag[i])
            xiaoAI.push_back(v[i]);
        else
            xiaoXI.push_back(v[i]);
    }

    long long ans = 0;
    for (int i = 0; i < xiaoAI.size(); ++i) {
        for (int j = 0; j < xiaoXI.size(); ++j) {
            if (xiaoAI[i] > xiaoXI[j])
                ans += xiaoAI[i] - xiaoXI[j];
            else
                ans += xiaoXI[j] - xiaoAI[i];
        }
    }
    cout << ans << endl;
    return 0;
}
